<?php
class ControllerArticleArticle extends ListController {

    protected function heading_title() {
        return "文章列表";
    }
    protected function primary_key_name() {
        return "ID";
    }
    protected function class_path() {
        return "article/article";
    }
    protected function model_name() {
        return "model_article_article";
    }

    public function get_province_list_with_city(){
        $this->load->model($this->class_path());

        $data = $this->model_article_article->get_province_list();

        foreach ($data as &$item){
            $item['items'] = $this->model_article_article->get_city_list($item['country_id']);

            foreach ( $item['items'] as &$city){
                $city['selected'] = false;
            }
        }

        echo json_encode($data);
    }

    public function get_province_list(){
        $this->load->model($this->class_path());

        $data = $this->model_article_article->get_province_list();

        echo json_encode($data);
    }
    public function get_city_list(){

        $this->load->model($this->class_path());

        $data = $this->model_article_article->get_city_list($this->request->get['parent_id']);

        echo json_encode($data);
    }

    public function preview_impl($id)
    {
        $this->load->model($this->class_path());

        $item = $this->model_article_article->getItem($id);
        $item['post_content'] = html_entity_decode($item['post_content']);
        $comments = $this->model_article_article->getComments($this->request->get['ID']);

        $table = "<table class='table table-bordered table-hover''>";
        $table .= "<thead>";
        $table .= "<tr>";
        $table .= "  <td class='text-left'><a>用户</a></td>";
        $table .= "      <td class='text-left'><a>发送时间</a></td>";
        $table .= "      <td class='text-left'><a>内容</a></td>";
        $table .= "      <td class='text-left'><a>操作</a></td>";
        $table .= "</tr> </thead><tbody>";

        foreach ($comments as $comment) {
            $table .= "<tr>";
            $table .= "<td class='text-left'><img style='width:30px;height:30px;' src='{$comment['image']}' /> {$comment['comment_author']}</td>";
            $table .= "<td class='text-left'>{$comment['comment_date']}</td>";
            $table .= "<td class='text-left'>{$comment['comment_content']}</td>";
            if ((int)$comment['comment_parent'] > 0){
                $table .= "<td class='text-left'></td>";
            }
            else{
                $table .= "<td class='text-left'><a onclick=\"comment({$id},{$comment['user_id']})\" title=\"回复\"><i class=\"fa \">回复</i></a></td>";
            }

            $table .= "</tr>";


        }

        $table .= "</tbody></table>";
        $item['post_comments'] = $table;
        return $item;
    }

    public function preview()
    {
        $item =  $this->preview_impl($this->request->get['ID']);

        $data['heading_title'] =  '预览';

        $data['text_form'] = '';
        $data['text_yes'] = $this->language->get('text_yes');
        $data['text_no'] = $this->language->get('text_no');

        $data['entry_name'] = $this->language->get('entry_name');
        $data['entry_description'] = $this->language->get('entry_description');
        $data['entry_approval'] = $this->language->get('entry_approval');
        $data['entry_sort_order'] = $this->language->get('entry_sort_order');

        $data['help_approval'] = $this->language->get('help_approval');

        $data['button_save'] = $this->language->get('button_save');
        $data['button_cancel'] = $this->language->get('button_cancel');

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['name'])) {
            $data['error_name'] = $this->error['name'];
        } else {
            $data['error_name'] = array();
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->heading_title(),
            'href' => $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $url, 'SSL')
        );

        $extra = $this->extra_params();
        if(utf8_strlen($extra)>0)
            $extra = '&' . $extra;

        if (!isset($this->request->get[$this->primary_key_name()])) {
            $data['action'] = $this->url->link($this->class_path().'/add', 'token=' . $this->session->data['token'] . $extra . $url, 'SSL');
            $data['primary_key'] = 0;
        } else {
            $data['action'] = $this->url->link($this->class_path().'/edit', 'token=' . $this->session->data['token'] . $extra . '&' . $this->primary_key_name() . '=' . $this->request->get[$this->primary_key_name()] . $url, 'SSL');
            $data['primary_key'] = $this->request->get[$this->primary_key_name()];
        }

        $data['cancel'] = $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . $extra . $url, 'SSL');



        $data['token'] = $this->session->data['token'];

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $data['extra'] = $item;

        $this->response->setOutput($this->load->view('article/preview.php', $data));


    }

    public function comment()
    {

        $this->load->model($this->class_path());
        $this->model_article_article->comment($this->request->get['ID'],$this->user->getUserName(),$this->request->get['customer_id'],$this->user->getId(),$this->request->get['message']);

        $item = $this->preview_impl($this->request->get['ID']);

        echo json_encode($item);
    }

    protected function get_list_data(&$ret,$filter_data,$url)
    {


        if (isset($this->request->get['filter_title'])) {
            $filter_data['filter_title'] = $this->request->get['filter_title'];
        } else {
            $filter_data['filter_title']  = null;
        }
        if (isset($this->request->get['filter_category'])) {
            $filter_data['filter_category'] = $this->request->get['filter_category'];
        } else {
            $filter_data['filter_category']  = null;
        }

        $status = 'publish';
        if(isset($_GET['status']) && $_GET['status'] == 'draft')
            $status = 'draft';
        $filter_data['status'] = $status;

        $total = $this->{$this->model_name()}->getCount($filter_data);
        $ret = $this->{$this->model_name()}->getList($filter_data);
        foreach ($ret as &$result) {
            $result['preview'] = $this->url->link($this->class_path().'/preview', 'token=' . $this->session->data['token'] . '&'.$this->primary_key_name(). '=' . $result[$this->primary_key_name()] . $url, 'SSL');
        }


        return $total;
    }

    protected function push_data(&$data){
        $this->load->model('article/term');

        $tmp = array();
        $tmp['taxonomy'] = 'category';
        $data['categories'] = $this->model_article_term->getList($tmp);


        if (isset($this->request->get['filter_title'])) {
            $data['filter_title'] = $this->request->get['filter_title'];
        } else {
            $data['filter_title'] = null;
        }


        if (isset($this->request->get['filter_category'])) {
            $data['filter_category'] = (int)$this->request->get['filter_category'];
        } else {
            $data['filter_category'] = null;
        }


        $data['publish_list'] = $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . "&status=publish"  );
        $data['draft_list'] = $this->url->link($this->class_path(), 'token=' . $this->session->data['token'] . "&status=draft" );

        $data['send'] = $this->url->link($this->class_path().'/send', 'token=' . $this->session->data['token'] );

        $status = 'publish';
        if(isset($_GET['status']) && $_GET['status'] == 'draft')
            $status = 'draft';

        $data['status'] = $status;
    }


    protected function get_form_data(&$data){
        $item = null;
        if (isset($this->request->get[$this->primary_key_name()]) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $item = $this->{$this->model_name()}->getItem($this->request->get[$this->primary_key_name()]);
            $data['post_status'] = $item['post_status'];
        }

        $this->load->model('tool/image');

        if (isset($this->request->post['post_title'])) {
            $data['post_title'] = $this->request->post['post_title'];
        } elseif (isset($item)) {
            $data['post_title'] = $item['post_title'];
        } else {
            $data['post_title'] = '';
        }

        if (isset($this->request->post['image'])) {
            $data['image'] = $this->request->post['image'];
        } elseif (isset($item)) {
            $data['image'] = $item['image'];
        } else {
            $data['image'] = '';
        }

        $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);

        if (is_file(DIR_IMAGE . $data['image'])) {
            $data['thumb'] = $this->model_tool_image->resize($data['image'], 100, 100);
        }
        else{
            $data['thumb'] = $data['placeholder'];
        }

        if (isset($this->request->post['post_content'])) {
            $data['post_content'] = $this->request->post['post_content'];
        } elseif (isset($item)) {
            $data['post_content'] = $item['post_content'];
        } else {
            $data['post_content'] = '';
        }

        if (isset($this->request->post['post_categories'])) {
            $data['post_categories'] = $this->request->post['post_categories'];
        } elseif (isset($item)) {
            $data['post_categories'] = $item['post_categories'];
        } else {
            $data['post_categories'] = array();
        }

        $this->load->model('article/term');


        $tmp = array();
        $tmp['taxonomy'] = 'category';
        $data['categories'] = $this->model_article_term->getList($tmp);
    }

    public function delete() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if(isset($this->request->post['send'])){
            $this->send();
        }
        else if(isset($this->request->post['send_to_city'])){
            $this->send_to_city();
        }
        else  if(isset($this->request->post['publish'])){
            $this->publish();
        }
        else{
            if (isset($this->request->post['selected']) && $this->validateDelete()) {
                foreach ($this->request->post['selected'] as $id) {
                    $this->{$this->model_name()}->delete($id);
                }

                $this->session->data['success'] = $this->language->get('text_success');

                $url = '';

                if (isset($this->request->get['sort'])) {
                    $url .= '&sort=' . $this->request->get['sort'];
                }

                if (isset($this->request->get['order'])) {
                    $url .= '&order=' . $this->request->get['order'];
                }

                if (isset($this->request->get['page'])) {
                    $url .= '&page=' . $this->request->get['page'];
                }
                $extra = $this->extra_params();
                if(utf8_strlen($extra)>0)
                    $extra = '&' . $extra;
                $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] .$extra . $url, 'SSL'));
            }

            $this->getList();
        }


    }

    public function send() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if (isset($this->request->post['selected']) && isset($this->request->post['grades'])) {
            $this->{$this->model_name()}->send_to_grades($this->request->post['selected'],$this->request->post['grades'],$this->user->getId(),$this->request->post['sendTime']);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }
            $extra = $this->extra_params();
            if(utf8_strlen($extra)>0)
                $extra = '&' . $extra;
            $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] .$extra . $url, 'SSL'));
        }

        $this->getList();


    }

    public function send_to_city() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if (isset($this->request->post['selected']) && isset($this->request->post['cities'])) {
            $this->{$this->model_name()}->send_to_cities($this->request->post['selected'],$this->request->post['cities'],$this->user->getId(),$this->request->post['sendTime_city']);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }
            $extra = $this->extra_params();
            if(utf8_strlen($extra)>0)
                $extra = '&' . $extra;
            $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] .$extra . $url, 'SSL'));
        }

        $this->getList();


    }
    public function publish() {

        $this->document->setTitle($this->heading_title());

        $this->load->model($this->class_path());

        if (isset($this->request->post['selected'])) {
            $this->{$this->model_name()}->publish($this->request->post['selected']);

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }
            $extra = $this->extra_params();
            if(utf8_strlen($extra)>0)
                $extra = '&' . $extra;
            $this->response->redirect($this->url->link($this->class_path(), 'token=' . $this->session->data['token'] .$extra . $url, 'SSL'));
        }

        $this->getList();


    }

    protected function validateForm() {
        if (!$this->user->hasPermission('modify', $this->class_path())) {
            $this->error['warning'] = $this->language->get('error_permission');
            return !$this->error;
        }

        if (utf8_strlen($_POST['post_title']) == 0) {
            $this->error['warning'] = '标题不能为空';
            return !$this->error;
        }

        return !$this->error;
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify',$this->class_path())) {
            $this->error['warning'] = $this->language->get('error_permission');
            return !$this->error;
        }



        return !$this->error;
    }
}